package org.jsola.hr.web;

import org.apache.commons.io.FilenameUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExcelKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocCheckItemAddDTO;
import org.jsola.hr.dto.SocCheckItemUpdateDTO;
import org.jsola.hr.query.SocCheckItemQuery;
import org.jsola.hr.service.ISocCheckItemService;
import org.jsola.hr.vo.SocCheckItemListVO;
import org.jsola.hr.vo.SocCheckItemVO;
import org.jsola.hr.vo.excel.SocCheckItemExportVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.validation.Valid;


/**
 * 校验子项表
 *
 * @author zhr
 */
@RestController("hrSocCheckItemController")
@RequestMapping(HrConstants.API_PATH + "/v1/socCheckItems")
public class SocCheckItemController implements ISocCheckItemControllerAPI {

    @Autowired
    private ISocCheckItemService socCheckItemService;

    @Override
    @PostMapping
    public Result<SocCheckItemVO> save(@Valid @RequestBody SocCheckItemAddDTO socCheckItemAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增校验子项表权限校验

        return Result.success(
                socCheckItemService.save(socCheckItemAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocCheckItemUpdateDTO socCheckItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改校验子项表权限校验

        int count = socCheckItemService.update(socCheckItemUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{socCheckItemId}")
    public Result<Integer> delete(@PathVariable String socCheckItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除校验子项表权限校验

        int count = socCheckItemService.deleteByIds(tokenUser, socCheckItemId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socCheckItemIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除校验子项表权限校验

        int count = socCheckItemService.deleteByIds(tokenUser, socCheckItemIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socCheckItemId}")
    public Result<SocCheckItemVO> findById(@PathVariable String socCheckItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看校验子项表权限校验

        return Result.success(
                socCheckItemService.selectById(socCheckItemId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocCheckItemListVO>> listAll(@Valid SocCheckItemQuery socCheckItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看校验子项表权限校验

        return Result.success(
                socCheckItemService.select(socCheckItemQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocCheckItemListVO>> page(@Valid SocCheckItemQuery socCheckItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socCheckItemService.selectPage(socCheckItemQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/export")
    public ResponseEntity<Resource> export(@Valid SocCheckItemQuery socCheckItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 导出权限校验

        String excelFilePath = socCheckItemService
                .exportPathBySocCheckId(socCheckItemQuery, tokenUser);
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath)
                .getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }


}

